<?php
class Curourts_model extends CRM_Model{
	
	public function __construct(){
		parent::__construct();
	}
	
	public function GetDS(){
		$this->db->select('_curourts.rid as rid, _curourts.curourt_name as curourt_name,  
							_curourts.curourt_name_lat as curourt_name_lat,
							_countries.country_name as country_name,  
							_curourts.modifyDT as modifyDT, 
							_curourts.descr as descr, _curourts.archive');
		$this->db->from('_curourts');
		$this->db->join('_countries','_curourts._countries_rid = _countries.rid');
		if($sort = $this->ciObject->GetSessionData('sort')){
			$this->db->orderby($sort['c'], $sort['r']);
		}
		$query = $this->DBGet('_curourts');
		if($query->num_rows()>0) return $this->ciObject->GetPagingDS($query->result());
		return array();
	}
	
	public function GetCountriesList(){
		$this->db->select('_countries.rid, _countries.country_name as country_name');
		$this->db->from('_countries');
		$this->db->orderby('country_name');
		$query = $this->DBGet('_countries');
		if($query->num_rows()>0) return $query->result();
		return False;
	}
	
	public function GetEditDS(){
		$this->db->select('_curourts.rid as rid, _curourts.curourt_name as curourt_name,  
							_curourts.curourt_name_lat as curourt_name_lat,
							_curourts._countries_rid as _countries_rid,							
							_curourts.modifyDT as modifyDT, 
							_curourts.descr as descr, _curourts.archive');
		$this->db->from('_curourts');
		$this->db->where(array('rid'=>$this->ciObject->GetSessionData('activerecord')));
		$query = $this->DBGet('_curourts');
		if($query->num_rows()>0) return $query->row();
		return False;
	}
	
	public function CreateRecord(){
		$insArray = array('curourt_name'=>$_POST['curourt_name'],
							'curourt_name_lat'=>$_POST['curourt_name_lat'],
							'_countries_rid'=>$_POST['_countries_rid'],		
							'descr'=>$_POST['descr'],
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);
		$this->db->trans_begin();
		$this->db->insert('_curourts', $insArray);
		$insRid = $this->db->insert_id();
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return $insRid;
		}		
	}
	
	public function UpdateRecord(){
		$updateArray = array('curourt_name'=>$_POST['curourt_name'],
							'curourt_name_lat'=>$_POST['curourt_name_lat'],
							'_countries_rid'=>$_POST['_countries_rid'],		
							'descr'=>$_POST['descr'],
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);
		$this->db->trans_begin();
		$this->db->update('_curourts', $updateArray, array('rid'=>$this->ciObject->GetSessionData('activerecord')));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return True;
		}		
	}
	
	public function DeleteRecord(){
		$this->db->trans_begin();
		$this->db->delete('_curourts', array('rid'=>$this->ciObject->GetSessionData('activerecord')));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return True;
		}		
	}
}
?>